<?php
/*
 * @Homepage for application
 * @verion 1.0
 * @author Roshan
 */
class Application extends base {
	
	protected $_view;
	
	public function __construct() {
		$this->setView ();
	
	}
	public function run($view = '') {
		$this->setView ( $view );
		
		// Do business Logic
		$category = Model_Mapper_Categories::newInstance ()->getBySlug ( Params::getParam ( 'slug' ) );
		view::newInstance ()->_exportVariableToView ( 'current_category', $category );
		$breadcrumb = $category->toRoot ();
		
		foreach ( $breadcrumb as $item ) {
			breadcrumb::newInstance ()->add ( $item->getName (), $item->getURI () );
		}
		
		// pass category slug array as params to get items;
		

		if (Params::getParam ( 'location' )) {
			$loc = Params::getParam ( 'location' );
			$locations = Model_Mapper_Locations::newInstance ()->getChildrens ( $loc->getLocation_id (), true );
			$loc_items = array ();
			$loc_items [] = $loc->getLocation_id ();
			foreach ( $locations as $loc ) {
				$loc_items [] = $loc ['location_id'];
			}
			$items = $category->getItems ( true, array ('location_id' => $loc_items, 'active' => 1 ) );
		} else {
			$items = $category->getItems ( true, array ('active' => 1 ) );
		}
		
		//$t=$category->toTree();
		view::newInstance ()->_exportVariableToView ( 'items', $items );
		view::newInstance ()->_exportVariableToView ( 'main_categories', $category->tree () );
		unset ( $items );
		if ($category->getParent_id () == 0) {
			// Load Custom Cateogry Landing Page
			$this->dispatch ( 'category_landing.phtml' );
		} else {
			$this->dispatch ( 'category.phtml' );
		}
	
	}

}